<?php
/**
 * Validierungsklasse zur Parametervalidierung
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


/**
 * $val muss einem String mit vorgegebener Unteren- und/oder Oberen Zeichenlaenge entsprechen
 * */
Class Yapary_Parameter_CheckStringClean Extends Yapary_Exception_Error Implements Yapary_Parameter_ICheck
{
	private $min, $max;
	function __construct ( $_minLength = NULL, $_maxLength = NULL )
	{
		$this->min = $_minLength;
		$this->max = $_maxLength;
	}


	public function typedef ( $val )
	{
		return (string) $val;
	}


	public function getPattern ()
	{
		$pattern = NULL;
		if ($this->min !== NULL || $this->max !== NULL) {
			$pattern = '^.{'.$this->min.','.$this->max.'}$';
		} else {
			$pattern = '^.*$';
		}

		return $pattern;
	}


	public function isValid ( $val )
	{
		if (!isset($val)) {
			$this->addError(Yapary_Exception_Error::E_UNDEFINED);
			return false;
		}
		if (!is_string($val)) {
			$this->addError(Yapary_Exception_Error::E_WRONG_FORMAT);
			return false;
		}
		if ($this->max !== NULL && strlen($val) > $this->max) {
			$this->addError(Yapary_Exception_Error::E_MAX_LENGTH);
			return false;
		}
		if ($this->min !== NULL && strlen($val) < $this->min) {
			$this->addError(Yapary_Exception_Error::E_MIN_LENGTH);
			return false;
		}

		if (preg_match('/(\<[^>]*\>)/', $val)) { // RegEx muss in diesem Fall direkt hier implementiert sein
			$this->addError(Yapary_Exception_Error::E_ILLEGAL_TOKKEN);
			return false;
		}

		return true;
	}
}
